package virtualinit.util;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class SSHUtil {

    static {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> closeSSH()));
    }

    private static List<Session> sessions = new ArrayList<>();

    public static String prodSshIp = "10.44.0.1";
    public static String prodSshUser = "root";
    public static String prodSshPwd = "4PHuFxcd9q";
    public static String prodStorageIp = "pc-wz9829b7613p06aq9.mysql.polardb.rds.aliyuncs.com";
    public static String prodScheduleIp = "pc-wz94own66bn4837oo.mysql.polardb.rds.aliyuncs.com";

    public static String uatSshIp = "10.82.99.61";
    public static String uatSshUser = "tf3prd";
    public static String uatSshPwd = "mPmc2rYeqX";
    public static String uatStorageIp = "10.82.99.61";
    public static String uatScheduleIp = "10.82.99.62";


    public static void main(String[] args) throws Exception {
        openProdStorageSSH();
        // openProdScheduleSSH();
        openUatScheduleSSH();
        openUatStorageSSH();
        System.out.println();
    }

    private static Connection getUatConn() {
        return JdbcUtil.getConnection("jdbc:mysql://localhost:9527/tf-domain-storage?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT",
                "root", "uCaUm6YOjh9OFyRl");
    }

    private static Connection getProdConn() {
        return JdbcUtil.getConnection("jdbc:mysql://localhost:9527/tf-domain-storeage",
                "tf3_operation2", "MF#FHnBEuQ");
    }


    public static void openProdScheduleSSH() throws Exception {
        openSSH(prodSshIp, prodSshUser, prodSshPwd, 9527, prodScheduleIp);
    }

    public static void openProdStorageSSH() throws Exception {
        openSSH(prodSshIp, prodSshUser, prodSshPwd, 9528, prodStorageIp);
    }

    public static void openUatScheduleSSH() throws Exception {
        openSSH(uatSshIp, uatSshUser, uatSshPwd, 9527, uatScheduleIp);
    }

    public static void openUatStorageSSH() throws Exception {
        openSSH(uatSshIp, uatSshUser, uatSshPwd, 9528, uatStorageIp);
    }


    public static void openSSH(String ip, String user, String pwd, int localPort, String forwardIp) throws Exception {
        Session session;
        JSch jSch = new JSch();
        session = jSch.getSession(user, ip, 22);
        session.setPassword(pwd);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        session.setPortForwardingL(localPort, forwardIp, 3306);
        System.out.println("打开SSH， " + getSSHInfo(session));
        sessions.add(session);
    }

    public static void closeSSH() {
        sessions.forEach(e -> {
            try {
                System.out.println("关闭ssh：" + getSSHInfo(e));
                e.disconnect();
            } catch (Exception e1) {

            }
        });
    }

    private static String getSSHInfo(Session e) throws JSchException {
        String[] s = e.getPortForwardingL()[0].split(":");
        return String.format("SSH信息： version:%s  转发信息： localhost:%s  -->  SSH %s:%s  -->  Forward %s:%s",
                e.getServerVersion(), s[0], e.getHost(), e.getPort(), s[1], s[2]);
    }

    public static boolean hasSession() {
        return !sessions.isEmpty();
    }
}

